package bhbt.controller.member.info;


import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import bhbt.controller.core.BaseController;
import bhbt.model.Admin;
import bhbt.model.Member;
import bhbt.service.MemberService;
import bhbt.utils.ContextUtil;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;
import bhbt.utils.SysConst;






/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
 
public class UpdateInfoController extends BaseController {
    @Resource
    private MemberService memberService;
   
   
    @RequestMapping("member/info/updatePwdShow.shtml")
    public String updatePwdShow(ModelMap model,HttpServletRequest request,HttpSession session) throws Exception{
        
		return "member/info/updatePwd";
    	
        
        
    }
    
    @RequestMapping("member/info/updatePwd.shtml")
    public String updatePwd(ModelMap model,HttpServletRequest request,HttpSession session) throws Exception{
    	Map formMap = getParameterMap(request);
        
        Member member = ContextUtil.getMember();
        String myTwoPassword = MapUtil.getString(formMap, "yuanTwoPassword");
   	 	String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
   	 	
   	 	if(!member.getTwoPassword().equals(pwd)){
    		setMessage(model,"原安全密码不正确。");
    		return "member/info/updatePwd";
    		
    	}else{
    		//修改密码
    		String onePassword = MapUtil.getString(formMap, "onePassword");
    		String onePasswordRec = MapUtil.getString(formMap, "onePasswordRec");
    		String twoPassword = MapUtil.getString(formMap, "twoPassword");
    		String twoPasswordRec = MapUtil.getString(formMap, "twoPasswordRec");
    		
    		if(onePassword.length()<=0||twoPassword.length()<=0){
    			setMessage(model,"请输入新密码。");
        		return "member/info/updatePwd";
    		}else{
    			if(!onePassword.equals(onePasswordRec)){
    				setMessage(model,"两次登陆密码不一致");
            		return "member/info/updatePwd";
    			}
    			if(!twoPassword.equals(twoPasswordRec)){
    				setMessage(model,"两次安全密码不一致");
            		return "member/info/updatePwd";
    			}
    			
    			String twoPasswordKey = CryptUtil.getInstance().encrypt2String(twoPassword,CryptUtil.keyStr);
        	 	String onePasswordKey = CryptUtil.getInstance().encrypt2String(onePassword,CryptUtil.keyStr);
        	 	
        	 	
        		setMessage(model,"修改成功。");
        		Boolean result = true;
        		request.setAttribute("result", result);
        		//修改缓存
        		
        		Map mapMember = new HashMap();
        		mapMember.put("id",member.getId());
        		mapMember.put("onePassword",onePasswordKey);
        		mapMember.put("twoPassword",twoPasswordKey);
        		memberService.update(mapMember);
        		
        		Member member2 = memberService.getByKey(member.getId());
        		session.setAttribute(SysConst.MEMBER, member2);
        		
        		return "member/info/updatePwd";
    		}
    		
       	 	
    	 	
    	}
        
        
    }
    

    @RequestMapping("member/info/updateSelfShow.shtml")
    public String updateSelfShow(ModelMap model,HttpServletRequest request,HttpSession session) throws Exception{
        Map formMap = getParameterMap(request);
        Member member = ContextUtil.getMember();
        
        member=memberService.getByKey(member.getId());
        request.setAttribute("member",member);
		//修改缓存
		
		return "member/info/updateSelf";
    	
        
        
    }
    
    @RequestMapping("member/info/updateSelf.shtml")
    public String updateSelf(ModelMap model,HttpServletRequest request,HttpSession session) throws Exception{
        Map formMap = getParameterMap(request);
       
        Member member = ContextUtil.getMember();
        String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
   	 	String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
   	 	
   	 	if(!member.getTwoPassword().equals(pwd)){
    		setMessage(model,"安全密码不正确。");
    		request.setAttribute("member",member);
    		return "member/info/updateSelf";
    		
    	}else {
    		Map mapMember = new HashMap();
        	mapMember.put("id",member.getId());
        	mapMember.put("title",MapUtil.getString(formMap, "title"));
        	mapMember.put("tel",MapUtil.getString(formMap, "tel"));
        	mapMember.put("qq",MapUtil.getString(formMap, "qq"));
        	mapMember.put("wei",MapUtil.getString(formMap, "wei"));
        	mapMember.put("email",MapUtil.getString(formMap, "email"));
        	mapMember.put("personName",MapUtil.getString(formMap,"personName"));
        	mapMember.put("cardNo",MapUtil.getString(formMap,"cardNo"));
        	mapMember.put("remark",MapUtil.getString(formMap,"remark"));
        	mapMember.put("bankName",MapUtil.getString(formMap, "bankName"));
    		mapMember.put("bankAddress",MapUtil.getString(formMap, "bankAddress"));        	
    		mapMember.put("bankCardNo",MapUtil.getString(formMap, "bankCardNo"));
    		mapMember.put("bankSheng",MapUtil.getString(formMap, "bankSheng"));
    		mapMember.put("bankShi",MapUtil.getString(formMap, "bankShi"));
    		mapMember.put("bankPersonName",MapUtil.getString(formMap, "bankPersonName"));
        	
        	mapMember.put("zhifuName",MapUtil.getString(formMap, "zhifuName"));
        	mapMember.put("zhifuCardNo",MapUtil.getString(formMap, "zhifuCardNo"));
        	mapMember.put("stateZiliao","T");
        	memberService.update(mapMember);
    		setMessage(model,"修改成功。");
    	}
    	
		
		 
		member=memberService.getByKey(member.getId());
		 
		request.setAttribute("member",member);
      
		return "member/info/updateSelf";
    	
        
        
    }
    
    @RequestMapping("member/info/updateBankShow.shtml")
    public String updateBankShow(ModelMap model,HttpServletRequest request,HttpSession session) throws Exception{
        Map formMap = getParameterMap(request);
        Member member = ContextUtil.getMember();
        
        member=memberService.getByKey(member.getId());
        request.setAttribute("member",member);
		//修改缓存
		
		return "member/info/updateBank";
    	
        
        
    }
    
    @RequestMapping("member/info/updateBank.shtml")
    public String updateBank(ModelMap model,HttpServletRequest request,HttpSession session) throws Exception{
        Map formMap = getParameterMap(request);
        Member member = ContextUtil.getMember();
        member=memberService.getByKey(member.getId());
        
       
        String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
   	 	String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
   	 	
   	 	if(!member.getTwoPassword().equals(pwd)){
    		setMessage(model,"安全密码不正确。");
    		request.setAttribute("member",member);
    		return "member/info/updateBank";
    		
    	}else {
//    		if(member.getStateZiliao().equals("T")){
//        		setMessage(model,"银行资料已经绑定，无法再修改。");
//        		request.setAttribute("member",member);
//        		return "member/info/updateBank";
//        	}else{
        		Map mapMember = new HashMap();
        		mapMember.put("id",member.getId());
        		mapMember.put("bankName",MapUtil.getString(formMap, "bankName"));
        		mapMember.put("bankAddress",MapUtil.getString(formMap, "bankAddress"));        	
        		mapMember.put("bankCardNo",MapUtil.getString(formMap, "bankCardNo"));
        		mapMember.put("bankSheng",MapUtil.getString(formMap, "bankSheng"));
        		mapMember.put("bankShi",MapUtil.getString(formMap, "bankShi"));
        		mapMember.put("bankPersonName",MapUtil.getString(formMap, "bankPersonName"));
            	
            	mapMember.put("zhifuName",MapUtil.getString(formMap, "zhifuName"));
            	mapMember.put("zhifuCardNo",MapUtil.getString(formMap, "zhifuCardNo"));
            	mapMember.put("stateZiliao","T");
            	memberService.update(mapMember);
            	Boolean result = true;
        		request.setAttribute("result", result);
        		setMessage(model,"银行资料绑定成功。"); 
        		member=memberService.getByKey(member.getId());
        		request.setAttribute("member",member);
        		session.setAttribute(SysConst.MEMBER, member); 
        		
        	//}
		}
        	
    	  	
		return "member/info/updateBank";
    	
        
        
    }
     

     
    

}
